package br.com.j7.jdesk.persistence.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import br.com.j7.jdesk.persistence.listener.CustomerListener;

/**
 * Entidade Customer
 * 
 * @author Edivando Alves
 *
 */
@Entity
@EntityListeners(value={CustomerListener.class})
@NamedQueries({@NamedQuery(name="customer", query="Select c from Customers c"),
			   @NamedQuery(name="customerByEnterprise", query="from Customers where enterprise = ?1"),
			   @NamedQuery(name="customerByCdCustomerAndEnterprise", query="Select c from Customers c where c.cdCustomer = ?1 AND c.enterprise = ?2")})
public class Customers implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idCustomer;
	
	@JoinColumn(name="idCustomer")
	@OneToMany(mappedBy="customer", cascade = CascadeType.MERGE)
	private List<Services> services;
	@Column(length=120)
	private String cdCustomer;
	@Column(length=20)
	private String cdCPF;
	@Column(length=150)
	private String cdAddress;
	@Column(length=100)
	private String cdNeighborhood;
	@Column(length=100)
	private String cdCity;
	@Column(length=20)
	private String cdSex;
	@Column(length=20)
	private String cdCEP;
	@Column(length=20)
	private String cdPhone;
	@Column(length=100)
	private String cdEmail;
	@Temporal(value=TemporalType.DATE)
	private Date dtBirthday; 
	@ManyToOne
	@JoinColumn(name="idEnterprise")
	private Enterprises enterprise;
	
	@OneToMany(mappedBy="customer", cascade=CascadeType.MERGE)
	private List<Sales> buy;
	
	@Version
	private Integer nrVersion;
	
	public Customers() {
	}
	
	public Customers(Integer idCustomer){
		this.idCustomer = idCustomer;
	}

	public Integer getIdCustomer() {
		return idCustomer;
	}
	
	public void setIdCustomer(Integer idCustomer) {
		this.idCustomer = idCustomer;
	}
	
	public List<Services> getServices() {
		if(services == null){
			services = new ArrayList<Services>();
		}
		return services;
	}
	
	public void setServices(List<Services> services) {
		this.services = services;
	}

	public String getCdCustomer() {
		return cdCustomer;
	}

	public void setCdCustomer(String cdCustomer) {
		this.cdCustomer = cdCustomer;
	}

	public String getCdPhone() {
		return cdPhone;
	}

	public void setCdPhone(String cdPhone) {
		this.cdPhone = cdPhone;
	}

	public String getCdEmail() {
		return cdEmail;
	}

	public void setCdEmail(String cdEmail) {
		this.cdEmail = cdEmail;
	}
	
	public String getCdCPF() {
		return cdCPF;
	}

	public void setCdCPF(String cdCPF) {
		this.cdCPF = cdCPF;
	}

	public String getCdAddress() {
		return cdAddress;
	}

	public void setCdAddress(String cdAddress) {
		this.cdAddress = cdAddress;
	}

	public String getCdNeighborhood() {
		return cdNeighborhood;
	}

	public void setCdNeighborhood(String cdNeighborhood) {
		this.cdNeighborhood = cdNeighborhood;
	}

	public String getCdCity() {
		return cdCity;
	}

	public void setCdCity(String cdCity) {
		this.cdCity = cdCity;
	}

	public String getCdSex() {
		return cdSex;
	}

	public void setCdSex(String cdSex) {
		this.cdSex = cdSex;
	}

	public String getCdCEP() {
		return cdCEP;
	}

	public void setCdCEP(String cdCEP) {
		this.cdCEP = cdCEP;
	}
	
	public Date getDtBirthday() {
		return dtBirthday;
	}
	
	public void setDtBirthday(Date dtBirthday) {
		this.dtBirthday = dtBirthday;
	}
	
	public Integer getNrVersion() {
		return nrVersion;
	}
	
	public void setNrVersion(Integer nrVersion) {
		this.nrVersion = nrVersion;
	}
	
	public Customers addService(Services service){
		service.setCustomer(this);
		getServices().add(service);
		return this;
	}
	
	public Enterprises getEnterprise() {
		return enterprise;
	}
	
	public void setEnterprise(Enterprises enterprise) {
		this.enterprise = enterprise;
	}
	
	public List<Sales> getBuy() {
		return buy;
	}
	
	public void setBuy(List<Sales> buy) {
		this.buy = buy;
	}
	
	@Override
	public String toString() {
		return cdCustomer;
	}

	@Override
	public int hashCode() {
		return idCustomer;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Customers other = (Customers) obj;
		if (idCustomer == null) {
			if (other.idCustomer != null)
				return false;
		} else if (!idCustomer.equals(other.idCustomer))
			return false;
		return true;
	}
	
	
	
}
